import axios, { AxiosRequestConfig } from 'axios';

const { VUE_APP_API_HOST } = process.env;

axios.defaults.baseURL = `${VUE_APP_API_HOST}/api`;
axios.interceptors.response.use((response) => response.data, (error) => {
    throw error;
});

let authHeader: AxiosRequestConfig | null = null;

export function setAuth(header?: AxiosRequestConfig) {
    authHeader = header || {};

    if (header) {
        window.localStorage.setItem('tokenHeader', JSON.stringify(header));
    } else {
        window.localStorage.removeItem('tokenHeader');
    }
}

export function getAuth() {
    return authHeader || JSON.parse(window.localStorage.getItem('tokenHeader') || '') || {};
}

export function getFileUploadAuth() {
    const header = {
        headers: {
            Authorization: authHeader?.headers.Authorization,
            // 'Access-Control-Allow-Origin': '*',
            'Content-Type': 'multipart/form-data',
        },
    };

    return header;
}
